#!/usr/bin/python
# -*-coding:utf-8-*-

import re
from django.core.validators import RegexValidator

username_re = re.compile(r'^([\w]{9}|[a-zA-Z]{1}[\w]+?)$')
username = RegexValidator(username_re, '管理员:4-12位,由字母数字下划线组成,首字母为字母', 'invalid')

# password_re = re.compile(r'([^a-z0-9A-Z])+')

# password_re = re.compile(r'^[0-9a-zA-Z~!@#$%^&*()_+.]{6,16}$')
password_re = re.compile(
    r'^(?=.*\d)(?=.*[a-zA-Z])(?=.*[`~!@#$%^&*()_\-+=<>?:"{}|,.\/;\'\\[\]·~！@#￥%……&*（）——\-+={}|《》？：“”【】、；‘’，。、])[\da-zA-Z`~!@#$%^&*()_\-+=<>?:"{}|,.\/;\'\\[\]·~！@#￥%……&*（）——\-+={}|《》？：“”【】、；‘’，。、]{8,16}$')

password = RegexValidator(password_re, '密码由字母数字特殊符号组成的字符串，最少为8位', 'invalid')


def checklen(pwd):
    """检查长度"""
    return len(pwd) >= 8


def checkContainUpper(pwd):
    """ 检查包含大写字母 """
    pattern = re.compile('[A-Z]+')
    match = pattern.findall(pwd)
    if match:
        return True
    else:
        return False


def checkContainNum(pwd):
    """ 检查包含数字 """
    pattern = re.compile('[0-9]+')
    match = pattern.findall(pwd)
    if match:
        return True
    else:
        return False


def checkContainLower(pwd):
    """ 检查包含小写字母 """
    pattern = re.compile('[a-z]+')
    match = pattern.findall(pwd)
    if match:
        return True
    else:
        return False


def checkSymbol(pwd):
    """ 检查包含大小写字母数字 """
    pattern = re.compile('([^a-z0-9A-Z])+')
    match = pattern.findall(pwd)
    if match:
        return True
    else:
        return False


def checkPassword(pwd):
    lenOK = checklen(pwd)
    # upperOK = checkContainUpper(pwd)
    lowerOK = checkContainLower(pwd)
    numOK = checkContainNum(pwd)
    symbolOK = checkSymbol(pwd)
    return (lenOK and lowerOK and numOK and symbolOK)


def Checkpasswd(passwd):
    """ 检查密码合法性 """
    if checkPassword(passwd):
        return True
    else:
        return False


